package cn.qingchunjiaomu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class ViewPagerFlipActivity extends Activity implements OnPageChangeListener {
	private ViewPager vp;
    private List<View> views;
    private ImageView[] dots;
    private int currentIndex;
    private final int pageCount = 2;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initDots();
        InitViewPager();
    }
    
    // 初始化底部小圆点
    private void initDots() {
        LinearLayout cousorLayout = (LinearLayout) findViewById(R.id.cousorLayout);

        dots = new ImageView[pageCount];

        for (int i = 0; i < pageCount; i++) {
            dots[i] = (ImageView) cousorLayout.getChildAt(i);
            dots[i].setEnabled(true);
            dots[i].setTag(i);
        }

        currentIndex = 0;
        dots[currentIndex].setEnabled(false);
    }

    private void InitViewPager() {
		vp = (ViewPager) findViewById(R.id.vPager);
		views = new ArrayList<View>();
		views.add(newBtnPage());
		views.add(newBtnPage());
		vp.setAdapter(new ViewPagerAdapter(views));
		vp.setCurrentItem(0);
		vp.setOnPageChangeListener(this);
	}
    
    private View newBtnPage(){
    	LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.btn_page,null);
    	GridView gridview = (GridView) layout.findViewById(R.id.BtnPage);
    	
		SimpleAdapter saMenuItem = new SimpleAdapter(this, getPageItems(),R.layout.btn_item, new String[] { "ItemImage", "ItemText" }, // ��Ӧmap��Key
				new int[] { R.id.ItemImage, R.id.ItemText });
		
		gridview.setAdapter(saMenuItem);
		gridview.setOnTouchListener(forbidenScroll());
		gridview.setOnItemClickListener(clickFuncItem());
		
		return gridview;
    }
    
    private ArrayList<HashMap<String, Object>> getPageItems(){
		ArrayList<HashMap<String, Object>> memuList = new ArrayList<HashMap<String, Object>>();
		
		for (int i=0; i<12; i++) {
			memuList.add(newMenuItem("按钮", R.drawable.ic_launcher));
		}
			
		return memuList;
	}
    
    private HashMap<String, Object> newMenuItem(String text, int imgId){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ItemImage", imgId);
		map.put("ItemText", text);
		
		return map;
    }
    
    // 防止GridView自己滑动
    private OnTouchListener forbidenScroll(){
    	return (new OnTouchListener(){
		    @Override
		    public boolean onTouch(View v, MotionEvent event) {
		        if(event.getAction() == MotionEvent.ACTION_MOVE){
		            return true;
		        }
		        return false;
		    }

		});
    }
    
    // 按钮的点击事件处理
    private OnItemClickListener clickFuncItem(){
    	return (new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				//Intent intent = null;
				switch (arg2) {
				case 0:
					break;
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				case 5:
					break;
				case 6:
					break;
				case 7:
					break;
				case 8:
					break;
				case 9:
					break;
				default:
					break;
				}
			}
		});
    }
    
    /**
     *当前底部小圆点
     *
     */
    private void setCurDot(int positon)
    {
        if (positon < 0 || positon > pageCount - 1 || currentIndex == positon) {
            return;
        }

        dots[positon].setEnabled(false);
        dots[currentIndex].setEnabled(true);

        currentIndex = positon;
    }
    
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		setCurDot(arg0);
		
	}
}